package com.cong.project.job;


import com.cong.project.mapper.ContentMapper;
import com.cong.project.mapper.MerchantMessageMapper;
import com.cong.project.model.dto.ShopRankDto;
import com.cong.project.model.dto.TalkingRankDto;
import com.cong.project.model.enums.RankingEnum;
import com.cong.project.model.vo.ShopRankVo;
import com.cong.project.model.vo.TalkingRankVo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 缓存预热任务
 * @author lhc
 * @date 2022-09-15 15:33
 */
@Component
@Slf4j
public class PreCache {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Resource
    private ContentMapper contentMapper;

    @Resource
    private MerchantMessageMapper merchantMessageMapper;



    @Resource
    private RedissonClient redissonClient;




    /**
     * 每一个小时执行，更新排行榜数据
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void doCacheTalkRank(){
        Gson gson = new Gson();
        RLock lock = redissonClient.getLock("partner:preCacheTalkJob:doCache:%s");
        try {
            Long keepLive = 30000L;
            if (lock.tryLock(0,-1, TimeUnit.MILLISECONDS)) {
                System.out.println("getLock:"+Thread.currentThread().getId());
                List<TalkingRankVo>  talkingRankVos = contentMapper.getTalkingRank();
                for (TalkingRankVo talkingRankVo : talkingRankVos) {
                    TalkingRankDto talkingRankDto = new TalkingRankDto();
                    BeanUtils.copyProperties(talkingRankVo,talkingRankDto);
                    String json = gson.toJson(talkingRankDto);

                    //写缓存
                    try {
                        stringRedisTemplate.opsForZSet().add(RankingEnum.TALKING.getMessage(), json, talkingRankVo.getThumbNum());
                    } catch (Exception e) {
                        log.error("redis set key error",e);
                    }
                }

            }
        } catch (InterruptedException e) {
            log.error("doCacheRecommendUser 缓存预热错误 加锁错误：",e);
        }finally {
            //只能释放自己的锁
            if (lock.isHeldByCurrentThread()){
                System.out.println("unLock:"+Thread.currentThread().getId());

                lock.unlock();

            }
        }


    }

    /**
     * 每一个小时执行，更新排行榜数据
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void doCacheShopRank(){
        Gson gson = new Gson();
        RLock lock = redissonClient.getLock("partner:preCacheShopJob:doCache:%s");
        try {
            Long keepLive = 30000L;
            if (lock.tryLock(0,-1, TimeUnit.MILLISECONDS)) {
                System.out.println("getLock:"+Thread.currentThread().getId());
                List<ShopRankVo>  shopRankVos = merchantMessageMapper.getShopRank();
                for (ShopRankVo shopRankVo : shopRankVos) {
                    ShopRankDto shopRankDto = new ShopRankDto();
                    BeanUtils.copyProperties(shopRankVo,shopRankDto);
                    String json = gson.toJson(shopRankDto);

                    //写缓存
                    try {
                        stringRedisTemplate.opsForZSet().add(RankingEnum.SHOP.getMessage(), json, shopRankVo.getFansNum());
                    } catch (Exception e) {
                        log.error("redis set key error",e);
                    }
                }

            }
        } catch (InterruptedException e) {
            log.error("doCacheRecommendUser 缓存预热错误 加锁错误：",e);
        }finally {
            //只能释放自己的锁
            if (lock.isHeldByCurrentThread()){
                System.out.println("unLock:"+Thread.currentThread().getId());

                lock.unlock();

            }
        }


    }
}
